var _aojCanvas = null;
var _aojShowImageCenter = false;

function aojSetCanvas(canvasId) {
    _aojCanvas = document.getElementById(canvasId);
}

function aojCanvas() {
    return _aojCanvas;
}

function aojFillStyle(optionalNewValue) {
    var ctx = _aojCanvas.getContext('2d');
    if (optionalNewValue)
        ctx.fillStyle = optionalNewValue;
    return ctx.fillStyle;
}

function aojStrokeStyle(optionalNewValue) {
    var ctx = _aojCanvas.getContext('2d');
    if (optionalNewValue)
        ctx.strokeStyle = optionalNewValue;
    return ctx.strokeStyle;
}

function aojShowImageCenter(optionalNewValue) {
    if (optionalNewValue)
        _aojShowImageCenter = optionalNewValue;
    return _aojShowImageCenter;
}

function aojSetFont(fontStyle) {
    var ctx = _aojCanvas.getContext('2d');
    ctx.font = 'bold 14px sans-serif';
}

function aojDrawText(text, x, y, optionalFillStyle) {
    var ctx = _aojCanvas.getContext('2d');
    if (optionalFillStyle) {
        ctx.fillStyle = optionalFillStyle;
    }
    ctx.fillText(text, x, y);
}

function aojFillRect(x, y, w, h, optionalFillStyle) {
    var ctx = _aojCanvas.getContext('2d');
    ctx.beginPath();
    if (optionalFillStyle) {
        ctx.fillStyle = optionalFillStyle;
    }
    ctx.rect(x, y, w, h);
    ctx.fill();
}

function aojStrokeRect(x, y, w, h, optionalStrokeStyle) {
    var ctx = _aojCanvas.getContext('2d');
    ctx.beginPath();
    if (optionalStrokeStyle) {
        ctx.strokeStyle = optionalStrokeStyle;
    }
    ctx.rect(x, y, w, h);
    ctx.stroke();
}

function aojNewImage(imgURL, optionalLoadCallback) {
    var img = new Image();
    img.src = imgURL;
    img.onload = optionalLoadCallback;
    return img;
}

function aojDrawImage(img, x, y) {
    var ctx = _aojCanvas.getContext('2d');
    ctx.drawImage(img, x - img.width / 2, y - img.height / 2);
    if (_aojShowImageCenter) {
        ctx.arc(x, y, 2, 0, 2*Math.PI, false);
        ctx.fill();
    }
}

function aojDrawImageRotate(img, x, y, r) {
    var ctx = _aojCanvas.getContext('2d');
    ctx.translate(x, y);
    ctx.rotate(r);
    ctx.drawImage(img, -img.width / 2, -img.height / 2);
    ctx.rotate(-r);
    ctx.translate(-x, -y);
    if (_aojShowImageCenter) {
        ctx.arc(x, y, 2, 0, 2*Math.PI, false);
        ctx.fill();
    }
}

function aojDrawImageScale(img, x, y, sx, sy) {
    var ctx = _aojCanvas.getContext('2d');
    ctx.translate(x, y);
    ctx.scale(sx, sy);
    ctx.drawImage(img, -img.width / 2, -img.height / 2);
    ctx.scale(1/sx, 1/sy);
    ctx.translate(-x, -y);
    if (_aojShowImageCenter) {
        ctx.arc(x, y, 2, 0, 2*Math.PI, false);
        ctx.fill();
    }
}

function aojDrawImageRotateScale(img, x, y, r, sx, sy) {
    var ctx = _aojCanvas.getContext('2d');
    ctx.translate(x, y);
    ctx.scale(sx, sy);
    ctx.rotate(r);
    ctx.drawImage(img, -img.width / 2, -img.height / 2);
    ctx.rotate(-r);
    ctx.scale(1/sx, 1/sy);
    ctx.translate(-x, -y);
    if (_aojShowImageCenter) {
        ctx.arc(x, y, 2, 0, 2*Math.PI, false);
        ctx.fill();
    }
}